import { get } from "@/api/request";
import OSS from "ali-oss";
export async function file2base64(file) {
    return new Promise((r, j) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (evt) => {
            r(evt.target.result);
        };
        // reader.onerror = e => j(e);
    });
}
/**
 * oss上传文件
 * @param {string} type 文件类型 middle 私有文件 cdn 公共读文件 disk 归档文件
 * @param {string} fileName 文件名
 * @param {Object} file 文件
 * @param {Function} progress 上传进度回调
 * @param {Function} clientCall client 回调
 * client.cancel(); 取消上传
 * @returns {String} 文件路径
 */
export const ossUpload = async (
    type,
    fileName,
    file,
    progress = (p, checkpoint) => {},
    clientCall = (client) => {}
) => {
    const data = await get("/user/cu/sys/base/js-oss-up-auth", {
        type,
        fileName: fileName || file.name,
    });
    const client = new OSS(data);
    if (clientCall && typeof clientCall === "function") {
        clientCall(client);
    }
    const result = await client.multipartUpload(data.path, file, {
        progress,
    });
    return result.name;
};

export const downFile = async(
    type,
    fileName,
    resourceUrl
) => {
    const data = await get("/user/cu/sys/base/js-oss-up-auth", {
        type,
        fileName: fileName || file.name,
    });
    const client = new OSS(data);
    try {
        let result = await client.signatureUrl(resourceUrl);
        console.log(result);
    } catch (error) {
        console.log(error);
    }
}

/**
 * oss上传文件
 * @param {string} type 文件类型 middle 私有文件 cdn 公共读文件 disk 归档文件
 * @param {string} fileName 文件名
 * @param {Object} file 文件
 * @returns {String} 文件路径
 */
export const selectFileUpload = async (
    type,
    fileName,
    progress = (p, checkpoint) => {},
    preview = (file) => {},
    domSetting = (dom) => {},
    clientCall = (client) => {}
) => {
    const file = await new Promise((r, j) => {
        const dom = document.createElement("input");
        dom.type = "file";
        dom.style.display = "none";
        domSetting(dom);
        dom.onchange = (event) => {
            const files = event.target.files;
            if (files.length === 0) j(1);
            else r(files[0]);
        };
        dom.click();
    });
    let result = preview(file);
    if (result && typeof result.then === "function") {
        try {
            result = await result;
        } catch (error) {
            return;
        }
    }
    if (result === false) throw new Error("cancel");
    if (typeof result === "string") fileName = result;
    return ossUpload(type, fileName, file, progress, clientCall);
};

export const selectFile = (call, domSetting = (dom) => {}) => {
    const dom = document.createElement("input");
    dom.type = "file";
    dom.style.display = "none";
    domSetting(dom);
    dom.onchange = (event) => {
        if (event.target.files && event.target.files.length > 0) {
            call(event.target.files[0]);
        }
    };
    dom.click();
};
export const selectFiles = (call, domSetting = (dom) => {}) => {
    const dom = document.createElement("input");
    dom.type = "file";
    dom.multiple = "multiple";
    dom.style.display = "none";
    domSetting(dom);
    dom.onchange = (event) => {
        if (event.target.files && event.target.files.length > 0) {
            call(event.target.files);
        }
    };
    dom.click();
};
